﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif
public static class GameObjectExtensions
{
    public static List<GameObject> GetChildren(this GameObject mono)
    {
        var list = new List<GameObject>();
        for(int i = 0; i < mono.transform.childCount; i++)
        {
            list.Add(mono.transform.GetChild(i).gameObject);
        }
        return list;
    }

    public static List<T> GetAll<T>(this GameObject mono) where T:MonoBehaviour
    {
        List<T> ans = new List<T>();
        ans.AddRange(mono.GetComponents<T>());
        var childs = mono.GetChildren();
        foreach(var c in childs)
        {
            ans.AddRange(c.GetAll<T>());
        }
        return ans;
    }
#if UNITY_EDITOR
    public static GameObject GetPrefabObj(this GameObject obj)
    {
        if (PrefabUtility.IsOutermostPrefabInstanceRoot(obj))
        {
            //      Debug.Log(PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(obj));
            return AssetDatabase.LoadAssetAtPath<GameObject>(PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(obj));
       //     var path = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(obj).Substring(7).Split('.')[0];
         //   var prefab = Resources.Load<GameObject>(path);
        //    return prefab;
        }
        else return null;
    }
#endif

        public static string GetFullName(this Transform obj)
    {

        if (obj == null) return "";
        var par = obj.parent.GetFullName();
        if (string.IsNullOrEmpty(par)) return obj.name;
        else return par + "." + obj.name;
    }
}